\ProvidesClass{guji}[2006/12/10 v0.1a, by XING Zhaopeng zpxing@gmail.com]
%%%%%%%  original comment by zpxing %%%%%%%
%%本作品遵循 GPL 協議發布
                              %%To-do
                      %%2007/xx/xx---古籍結構
%%書衣
%%書簽
%%書腦
%%書脊
%%書頭
%%書根
%%扉頁
%%書名頁
%%牌記
%%序
%%目錄
%%凡例
%%正文
%%跋
%%夾注, 側注, 篇後注
                             %%History
                      %%2006/12/06---古籍版式
%%版面
%%版框
%%行格
%%版心
%%單魚尾, 雙魚尾
%%白口, 黑口
%%書耳
%%朱絲欄, 烏絲欄
                             %%已知問題
%%1. 文章如果在最左一格結束, 會在其後産生額外空白頁, 如果在其他格結束, 最後一頁不會輸出.
%%原因: 分格收集 \box255 的時候, 如果文章結束, 其後面下一個行格得到 \box255 是空的,
%%再后面的 \box255 究竟是 void, 還是empty? 我不知道 TeX 是怎么規定的.
%%2. 其他尺寸的漢字可能不會在行格中居中對齊.
%%原因: 我不知道漢字直排後一個字 \box 的 height 是不是原來的 width. 另外漢字的 pt 大小和其
%%三個 dimention 的關係, 在設計居中對齊時, 統一都按 pt 大小作爲一行直排漢字 \hbox 的height + depth
%%3. 改變 \textwidth 到某個時候, 會有莫明其妙的 \CJK@XX 類錯誤.
%%原因: 我定義的盒子很多都是固定尺寸的, 如果盒子裏內容的 glue 不够, 可能會錯誤.
%%4. 朱絲欄和底欄之間距離有點明顯.
%%原因: 朱絲欄使用 \textcolor{}{} 定義, 這樣就把裏面的 \vrule 給分組了(grouping), 所以會有小的空白.
%%5. 為什麽不使用計數器來 count 行格, 同時使用 \loop ... \if ... \repeat 來簡化 \shipout?
%%原因: 需要讓行格的定義爲全局的(\global), 但是 \global\setbox#1 似乎有問題, 會和其他 LaTeX
%%的內部盒子衝突.
%%6. 很多的\overfull \underfull 的警告.
%%原因: 我已經使用了很大的 \vfuzz \hfuzz, 但是同時消除 \overfull \underfull 是不現實的.
%%%%%%
%%%%%%%%%%%%%%%%  added comment by yindian %%%%%%%
%% \verticaltrue \verticalfalse set whether to use the vertical feature of fonts.
%% not tested in other font sizes.
%% XeTeX 3.141592-0.996 is buggy in handling fonts of FangSong_GB2312, KaiTi_GB2312, STKaiti, STFangsong, etc.
%% the source is terribly ugly.

\newdimen\GJ@fontsize
\DeclareOption{18pt}{\GJ@fontsize=17.28pt
\fontsize{\GJ@fontsize}{\baselineskip}\selectfont}
\DeclareOption{24pt}{\GJ@fontsize=24.88pt
\fontsize{\GJ@fontsize}{\baselineskip}\selectfont}
\DeclareOption{36pt}{\GJ@fontsize=35.83pt
\fontsize{\GJ@fontsize}{\baselineskip}\selectfont}
\newif\ifGJ@danbian
\newif\ifGJ@danyuwei
\newif\ifGJ@wusilan
\newif\ifGJ@shuerzi
\DeclareOption{danbian}{\GJ@danbiantrue}
\DeclareOption{shuangbian}{\GJ@danbianfalse}
\DeclareOption{danyuwei}{\GJ@danyuweitrue}
\DeclareOption{shuangyuwei}{\GJ@danyuweifalse}
\DeclareOption{wusilan}{\GJ@wusilantrue}
\DeclareOption{zhusilan}{\GJ@wusilanfalse}
\DeclareOption{shuerziWu}{\GJ@shuerzifalse}
\DeclareOption{shuerziYou}{\GJ@shuerzitrue}
\ExecuteOptions{24pt,shuangbian,shuangyuwei,zhusilan,shuerziYou}
\ProcessOptions

\LoadClass{book}
\RequirePackage{fontspec}
\newif\ifvertical
%\verticaltrue
\ifvertical
\defaultfontfeatures{RawFeature={vertical;script=hani}}
\else
\defaultfontfeatures{RawFeature={script=hani}}
\fi
\setmainfont[BoldFont=SimHei]{SimSun}
\setsansfont{SimHei}
\setmonofont{YouYuan}
\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 0.1em minus 0.01em
\def\@GJ@chaptertitle{}
\renewcommand{\chapter}[1]{\gdef\@GJ@chaptertitle{#1}}%\textbf{#1}
\renewcommand{\section}[1]{#1}
\parindent=0em

\newskip\@GJ@mainbarraise
\newskip\@GJ@banxinraise
\newskip\@GJ@shuerziraise
\@GJ@mainbarraise=-0.1\GJ@fontsize
\@GJ@banxinraise=0.7\GJ@fontsize
\@GJ@shuerziraise=0.15\GJ@fontsize
\def\@GJ@banxinfont{LiSu}
\def\@GJ@shuerfont{SimSun}
\def\setGJraise#1#2#3{\@GJ@mainbarraise=#1\GJ@fontsize\@GJ@banxinraise=#2\GJ@fontsize\@GJ@shuerziraise=#3\GJ@fontsize}
\def\setGJfont#1#2{\gdef\@GJ@banxinfont{#1}\gdef\@GJ@shuerfont{#2}}


\setlength\textheight{29pt}
\setlength\textwidth{7.134in}
\topskip=0pt
\vsize=\GJ@fontsize
%\maxdepth=1000pt
\newdimen\geheight
\newdimen\gewidth
\geheight=\textwidth
\gewidth=0.6\vsize
%\advance \gewidth by\baselineskip


\newbox\yuwei
\newdimen\yuweifineness
\yuweifineness=0.05pt
\newdimen\yuweineilength \newdimen\yuweiwailength
\yuweineilength=10pt \yuweiwailength=\yuweineilength
\advance \yuweiwailength by \gewidth
\global\setbox\yuwei=\vbox to 2\gewidth{%
    {\loop 
    \hrule width \yuweiwailength height \yuweifineness
    \advance\yuweiwailength by -\yuweifineness    
    \ifdim \yuweiwailength>\yuweineilength
    \repeat}
    {\loop 
    \hrule width \yuweineilength height \yuweifineness
    \advance \yuweineilength by \yuweifineness
    \ifdim \yuweineilength<\yuweiwailength
    \repeat}}

\newbox\jielan
\newdimen\jielanthickness
\jielanthickness=1pt
\newdimen\jielanheight
\advance \baselineskip by \jielanthickness
\jielanheight=.5\baselineskip
\newdimen\jielandepth
\advance \baselineskip by -2\jielanthickness
\jielandepth=-.5\baselineskip
\advance \baselineskip by \jielanthickness
\global\setbox\jielan=\hbox{%
    \ifGJ@wusilan
    \vrule height \jielanheight depth \jielandepth width \geheight
    \else
    \special{pdf: bc [ 0.8 0.2 0.2 ]}%
    \vrule height \jielanheight depth \jielandepth width \geheight
    \special{pdf: ec}%
    \fi}


\def\bankuang#1#2#3{%
    \vbox{\hrule height#3pt
        \hbox {%
            \vrule width#3pt \kern#2pt
            \vbox{\kern#2pt #1 \kern#2pt}%
            \kern#2pt\vrule width#3pt}
        \hrule height#3pt}}
\vfuzz=10000pt
\hfuzz=10000pt
\let\ge=a
\newbox\@tempboxgea \newbox\@tempboxgeb \newbox\@tempboxgec \newbox\@tempboxged \newbox\@tempboxgee
\newbox\@tempboxgef \newbox\@tempboxgeg \newbox\@tempboxgeh \newbox\@tempboxgei \newbox\@tempboxgej \newbox\@tempboxgek
\newbox\@tempboxgel \newbox\@tempboxgem \newbox\@tempboxgen \newbox\@tempboxgeo \newbox\@tempboxgep
%% output routine
\def\papersize{10.8in,7.8in}
\newif\if@firstpage@
\@firstpage@true
%\hoffset=-0.5in
%\voffset=-0.5in
\def\ChineseBox#1{\setbox0=\vbox{\boxmaxdepth=0pt
      #1}\dimen0=\wd0 \dimen2=\ht0
   \vbox to \dimen0{\hbox to \dimen2{\hfil
     \special{pdf:btrans rotate -90}\rlap{\vbox to 0pt
       {\box0\vss}}\special{pdf:etrans}}\vss}}
\output={%
    \if a\ge
    \global\setbox\@tempboxgea=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\box255\fi}
    %\ht\@tempboxgea=0.56in
    %\global\setbox\@tempboxgea=\hbox{\raise0.5\GJ@fontsize\vbox to \vsize{\unvbox255}}
    \global\let\ge=b
    \else
    \if b\ge
    \global\setbox\@tempboxgeb=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=c
    \else
    \if c\ge
    \global\setbox\@tempboxgec=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=d
    \else
    \if d\ge
    \global\setbox\@tempboxged=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=m
    \else
    \if m\ge
    \global\setbox\@tempboxgem=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=o
    \else
    \if o\ge
    \global\setbox\@tempboxgeo=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=e
    \else
    \if e\ge
    \global\setbox\@tempboxgee=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=f
    \else
    \if f\ge
    \global\setbox\@tempboxgef=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=g
    \else
    \if g\ge
    \global\setbox\@tempboxgeg=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=h
    \else
    \if h\ge
    \global\setbox\@tempboxgeh=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=i
    \else
    \if i\ge
    \global\setbox\@tempboxgei=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=n
    \else
    \if n\ge
    \global\setbox\@tempboxgen=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=p
    \else
    \if p\ge
    \global\setbox\@tempboxgep=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=j
    \else
    \if j\ge
    \global\setbox\@tempboxgej=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=k
    \else
    \if k\ge
    \global\setbox\@tempboxgek=\vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}
    \global\let\ge=l
    \else
    \newbox\banxin \newbox\shuer \newbox\banxinzi \newbox\shuerzi
    \global\setbox\banxinzi=\hbox{\vbox to 1.5\GJ@fontsize{%\renewcommand{\CJKglue}{\hskip \GJ@fontsize plus .1\baselineskip} 
      \ifvertical
      \vskip -\@GJ@banxinraise
      \fi
      \fontspec{\@GJ@banxinfont} \fontsize{1.3\GJ@fontsize}{\baselineskip}\selectfont {\@title} 
      \hskip.1\geheight 
      \fontspec{\@GJ@banxinfont} \fontsize{0.6\GJ@fontsize}{\baselineskip}\selectfont {\@GJ@chaptertitle}
      \ifvertical\vskip \@GJ@banxinraise\fi}}
    \global\setbox\shuerzi=\hbox{%\renewcommand{\CJKglue}{\hskip \GJ@fontsize plus 0.1\baselineskip} 
      \fontspec{\@GJ@shuerfont}\fontsize{0.5\GJ@fontsize}{\baselineskip}\selectfont {\@title}}
    \newdimen\heibaikou \newdimen\heibaikouheight \newdimen\heibaikoudepth
    \heibaikou=1pt
    \advance \gewidth by .5\heibaikou
    \heibaikouheight=\gewidth
    \advance \gewidth by -\heibaikou
    \heibaikoudepth=-\gewidth
    \advance \gewidth by .5\heibaikou
     \global\setbox\banxin=\vbox{\hrule height \jielanthickness \hbox to \geheight{%
     \ifGJ@danyuwei
      %\vrule height \heibaikouheight depth \heibaikoudepth width .2\geheight
      \hskip.2\geheight
      %\vrule height 2\gewidth \hskip.01\geheight 
        \vbox{\copy\yuwei}\hskip.01\geheight
        \vbox to 2\gewidth{\vfil \box\banxinzi \vfil}\hss
        \vrule height 2\gewidth \hskip.2\geheight
     \else
        \vrule height \heibaikouheight depth \heibaikoudepth width .2\geheight \vrule height 2\gewidth
        \hskip.01\geheight  \vbox{\copy\yuwei}
        \vbox to 2\gewidth{\vfil \box\banxinzi \vfil}\hss \vrule height 2\gewidth \hskip.01\geheight \vbox{\copy\yuwei}\kern-\yuweiwailength
        \advance \yuweiwailength by.2\geheight \vrule height \heibaikouheight depth \heibaikoudepth width \yuweiwailength \advance \yuweiwailength by-.2\geheight
     \fi}\hrule  height \jielanthickness}
    \ifGJ@shuerzi
     \global\setbox\shuer=\hbox{\kern5pt \vbox{\hbox{%
        \vrule height .4\gewidth
      \vbox to .4\gewidth{\vfil \ifvertical\vskip -\@GJ@shuerziraise\box\shuerzi\vskip \@GJ@shuerziraise
         \else\box\shuerzi\fi \vfil}
        \vrule height .4\gewidth}\hrule}}
    \fi
    \shipout\vbox{%
    \if@firstpage@
      \special{papersize=\papersize}%
      \special{pdf: bgcolor [0.5 0.7 0.8]}
      \@firstpage@false
    \fi
    \ChineseBox{%
    \ifGJ@danbian
     \bankuang{\vbox{%
    \vskip.5\baselineskip
    \box\@tempboxgea\vss\copy\jielan\vss
    \box\@tempboxgeb\vss\copy\jielan\vss
    \box\@tempboxgec\vss\copy\jielan\vss
    \box\@tempboxged\vss\copy\jielan\vss
    \box\@tempboxgem\vss\copy\jielan\vss
    \box\@tempboxgeo\vss\copy\jielan\vss
    \box\@tempboxgee\vss\copy\jielan\vss
    \box\@tempboxgef\vskip.5\baselineskip
    \vss\box\banxin\vss\vskip.5\baselineskip
    \box\@tempboxgeg\vss\copy\jielan\vss
    \box\@tempboxgeh\vss\copy\jielan\vss
    \box\@tempboxgei\vss\copy\jielan\vss
    \box\@tempboxgen\vss\copy\jielan\vss
    \box\@tempboxgep\vss\copy\jielan\vss
    \box\@tempboxgej\vss\copy\jielan\vss
    \box\@tempboxgek\vss\copy\jielan\vss
    \vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi}\vskip.5\baselineskip
    \global\let\ge=a}}12
    \vskip-2pt
    \box\shuer
    \else
    \bankuang{\bankuang{\vbox{%
    \vskip.5\baselineskip
    \box\@tempboxgea\vss\copy\jielan\vss
    \box\@tempboxgeb\vss\copy\jielan\vss
    \box\@tempboxgec\vss\copy\jielan\vss
    \box\@tempboxged\vss\copy\jielan\vss
    \box\@tempboxgem\vss\copy\jielan\vss
    \box\@tempboxgeo\vss\copy\jielan\vss
    \box\@tempboxgee\vss\copy\jielan\vss
    \box\@tempboxgef\vskip.5\baselineskip
    \vss\box\banxin\vss\vskip.5\baselineskip
    \box\@tempboxgeg\vss\copy\jielan\vss
    \box\@tempboxgeh\vss\copy\jielan\vss
    \box\@tempboxgei\vss\copy\jielan\vss
    \box\@tempboxgen\vss\copy\jielan\vss
    \box\@tempboxgep\vss\copy\jielan\vss
    \box\@tempboxgej\vss\copy\jielan\vss
    \box\@tempboxgek\vss\copy\jielan\vss
    \vbox to \vsize{\ifvertical\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise
    \else\vskip -\@GJ@mainbarraise\box255\vskip \@GJ@mainbarraise\fi} \vskip.5\baselineskip
    \global\let\ge=a}}12}35
    \vskip-2pt
    \box\shuer \fi
    }% ChineseBox
    }% vbox
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}

